<template>

  <ContentWrap>

    <!-- 搜索工作栏 -->
    <el-form
        class="-mb-15px"
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
        label-width="68px"
    >
      <el-form-item>
        <el-button @click="addwarrant"><Icon icon="ep:search" class="mr-5px" /> 增加权证员</el-button>
<!--        <el-button @click=""><Icon icon="ep:refresh" class="mr-5px" /> 批量删除</el-button>-->
<!--        <el-button @click=""><Icon icon="ep:refresh" class="mr-5px" /> 批量禁用</el-button>-->
      </el-form-item>
      <el-form-item label="姓名" prop="name" >
        <el-input
            v-model="queryParams.name"
            placeholder="请输入权证员姓名"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
        />
      </el-form-item>

      <el-form-item label="手机号" prop="mobile" >
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入手机号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>

      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>
  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true" border >
      <el-table-column label="ID" align="center" prop="id" width="100px" />
      <el-table-column
          label="创建时间"
          align="center"
          prop="createTime"
          :formatter="dateFormatter"
          width="200px"
      />
      <el-table-column label="用户头像" align="center"  width="150px" >
        <template #default="scope">
          <img :src="scope.row.avatar" style="width: 60px;height: 60px;border-radius: 4px;">
        </template>
      </el-table-column>
      <el-table-column label="用户昵称" align="center" prop="nickname" width="150px" />
      <el-table-column label="真实姓名" align="center" prop="name" width="150px" />
      <el-table-column label="手机号码" align="center" prop="mobile" width="150px" />
<!--      <el-table-column label="标签" align="center" prop="agentName" width="150px" >-->
<!--      </el-table-column>-->
<!--      <el-table-column label="账户状态" align="center" prop="status" width="140px" >-->
<!--        <template #default="scope">-->
<!--          <span v-if="scope.row.status==0">正常</span>-->
<!--          <span v-if="scope.row.status==1">禁用</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column align="center" label="账户状态" prop="status" width="100px">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="首页推荐" align="center" prop="isHome" width="100px" >
        <template #default="scope">
          <span v-if="scope.row.isHome==0">关闭</span>
          <span v-if="scope.row.isHome==1">开启</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="200px">
        <template #default="scope">
          <el-button
              link
              type="primary"
              @click="openForm('look', scope.row.id)"
              checkPermi="['client:users:look']"
          >
            详情
          </el-button>
          <el-button
              link
              type="primary"
              @click="openWarrantForm('权证员信息编辑', scope.row.id)"
              checkPermi="['client:salesman:look']"
          >
            编辑
          </el-button>
          <el-button
              link
              type="primary"
              @click="handleDelete(scope.row.id)"
              checkPermi="['system:users:delete']"
          >
            删除
          </el-button>
          <el-button
              link
              type="danger"
              @click="handleForbidden(scope.row.id)"
              checkPermi="['system:users:update']"
          >
            禁用
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <WarrantForm ref="warrantForm" @success="getList" />

</template>

<script setup lang="ts">
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import { ref } from 'vue';
import { dateFormatter } from '@/utils/formatTime'
import * as UsersApi from '@/api/hutai/certificateHolder'
import WarrantForm from "@/views/hutai/warrant/WarrantForm.vue";

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
defineOptions({ name: 'HutaiClientpoolPool' })


const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据


const addwarrant=()=>{
  push('/warrant/warrantAdd')
}

const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  id: null,
  mobile: null,
  status: null,
  nickname: null,
  avatar: null,
  name: null,
  userType: null,
  remark: null,
  censusRegister: null,
  registeredHk: null,
  referrer: null,
  age: null,
  sex: null,
  birthday: null,
  levelId: null,
  groupId: null,
  agentId: null,
  agentName: null,
  warrantId: null,
  warrantName: null,
  isHome: null,
  wealthValue: null,
  openId: null
})
const queryFormRef = ref() // 搜索的表单
const  warrantForm = ref();
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    queryParams.userType = 3;
    const data = await UsersApi.getUsersPage(queryParams)
    console.log(data,"权证员信息")
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await UsersApi.deleteUser(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

const handleForbidden = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm('是否禁用当前用户')
    // 发起删除
    await UsersApi.forbiddenUser(id)
    message.success('操作成功')
    // 刷新列表
    await getList()
  } catch {}
}


const { push } = useRouter()
const openForm = (type: string, id?: number) => {
  push('/warrant/warrantDetails?id='+id)
}


const openWarrantForm = (type: string, id?: number) => {
  warrantForm.value.open(type, id)
}



/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
